package com.tjetc;

import com.tjetc.dao.BankDao;
import com.tjetc.pojo.Bank;
import com.tjetc.utils.MyBatisUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author gaoyongxiang
 * @version v1.0
 * @date 2025/8/19
 */
@SuppressWarnings("all")
public class _3MybatisTest {
    // 测试插入
    @Test
    public void testInsert() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        BankDao bankDao = sqlSession.getMapper(BankDao.class);
        int row = bankDao.insertBank("zhangsan");
        sqlSession.commit();
        System.out.println(row);
    }

    // 测试自动提交
    @Test
    public void testInsertAutoCommit() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        BankDao bankDao = sqlSession.getMapper(BankDao.class);
        int row = bankDao.insertBank("lisi");
        System.out.println(row);
    }

    // 测试接收的数据类型为map
    @Test
    public void testHashMap() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        BankDao bankDao = sqlSession.getMapper(BankDao.class);
        Map map = bankDao.selectBank1(1);
        // 使用lambda表达式进行遍历
        map.forEach((k, v) -> {
            System.out.println("k.getClass()=" + k.getClass() + "k.value=" + k);
            System.out.println("v.getClass()=" + v.getClass() + "v.value=" + v);
        });
    }


    // 测试插入成功后返回主键
    @Test
    public void testInsertGetKey() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        BankDao bankDao = sqlSession.getMapper(BankDao.class);
        Bank bank = new Bank();
        bank.setName("wangwu");
        int row = bankDao.insertBank1(bank);
        System.out.println(row);
        System.out.println(bank);
    }

    /*
        测试接口方法的传递参数
        dao:      和数据库打交道 命名：select insert delete update
        service:   query（find） save（add） remove update
        controller: query（find）get  save    delete modify（update）
        queryUserList
     */
    @Test
    public void testParam() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        BankDao bankDao = sqlSession.getMapper(BankDao.class);
        // Error querying database.  错误查询数据库
        // Cause: org.apache.ibatis.binding.BindingException: 绑定异常
        // Parameter 'id' not found. Available parameters are [arg1, arg0, param1, param2]
        Bank bank = bankDao.selectBank2(1, "zhaoliu");
        System.out.println(bank);
    }

    // 使用trim和if标签
    @Test
    public void testDynamicsInsert() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        BankDao bankDao = sqlSession.getMapper(BankDao.class);
        Bank bank = new Bank();
        // 下面的两个数据全是外接传入的
        String name = "zhangsan";
        BigDecimal money = new BigDecimal("110");

//        if (StringUtils.isNoneBlank(name)) {
//            bank.setName(name);
//        }
//
//        if (ObjectUtils.isNotEmpty(money)) {
//            bank.setMoney(money);
//        }
//        bank.setName(name);
        bank.setMoney(money);
        bankDao.insertBank2(bank);
    }

    // 使用where标签
    @Test
    public void testDynamicsWhere() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        BankDao bankDao = sqlSession.getMapper(BankDao.class);
        Bank bank = new Bank();
        bank.setId(1);
        bank.setName("zhangsan");
        Bank bank1 = bankDao.selectBank3(bank);
        System.out.println(bank1);
    }

    // 使用choose...when
    @Test
    public void testDynamicsChoose() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        BankDao bankDao = sqlSession.getMapper(BankDao.class);
        Bank bank = new Bank();
        bank.setId(2);
        bank.setName("zhangsan");
        Bank bank1 = bankDao.selectBank4(bank);
        System.out.println(bank1);
    }

    // 使用set标签
    @Test
    public void testDynamicsSet() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        BankDao bankDao = sqlSession.getMapper(BankDao.class);
        BigDecimal money = new BigDecimal("110");
        Bank bank = new Bank();
        bank.setId(1);
        bank.setName("lisi");
//        bank.setMoney(money);
        int row = bankDao.updateBank(bank);
        System.out.println(row);
    }


    // 使用foreach标签
    @Test
    public void testDynamicsForeach() {
        SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
        BankDao bankDao = sqlSession.getMapper(BankDao.class);
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(2);
        List<Bank> banks = bankDao.selectBank5(ids);
        System.out.println(banks);

    }

}